﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Expressions;

namespace System.Web.Mvc
{
    public static class MOLHtmlHelperExtend
    {
        public static MvcHtmlString DatePicker(this HtmlHelper htmlhelper, string name)
        {
            TagBuilder tagBuilder = new TagBuilder("input");//设置标签类型为input
            tagBuilder.Attributes.Add("type", "text");//为标签添加type属性及值
            tagBuilder.Attributes.Add("name", name);//为标签添加name属性及值
            tagBuilder.Attributes.Add("onclick", "JTC.setday({readOnly :true})");
            tagBuilder.GenerateId(name);//为标签生成Id，name参数代码Id的值
            //创建经过HTML加密的字符串
            //TagRenderMode.SelfClosing枚举值代表当前标签是自动关闭的
            return MvcHtmlString.Create("<script src='../../Scripts/DatePicker.js'></script>"+tagBuilder.ToString(TagRenderMode.SelfClosing));
        }

        public static MvcHtmlString DatePickerFor<TModel,TValue>(this HtmlHelper<TModel> htmlhelper,Expression<Func<TModel,TValue>> expression)
        {
              string modelName = ExpressionHelper.GetExpressionText(expression);//从Lambda表达式中获取模型对应属性的名称
              ModelMetadata meta = ModelMetadata.FromLambdaExpression(expression, htmlhelper.ViewData);
              TagBuilder tagBuilder = new TagBuilder("input");//设置标签类型为input
             
              tagBuilder.Attributes.Add("type", "text");//为标签添加type属性及值
              tagBuilder.Attributes.Add("name", modelName);//为标签添加name属性及值
              tagBuilder.Attributes.Add("onclick", "JTC.setday({readOnly :true})");
              tagBuilder.Attributes.Add("value", meta.Model==null?"":meta.Model.ToString());
              tagBuilder.GenerateId(modelName);//为标签生成Id，name参数代码Id的值
              tagBuilder.MergeAttributes(htmlhelper.GetUnobtrusiveValidationAttributes(modelName, meta));
              //创建经过HTML加密的字符串
              //TagRenderMode.SelfClosing枚举值代表当前标签是自动关闭的
             return MvcHtmlString.Create("<script src='../../Scripts/DatePicker.js'></script>"+tagBuilder.ToString(TagRenderMode.SelfClosing));
        }
    }
}